/** @format */

import { computed, ref } from 'vue'

const navs = ref<any>([])
export const navUrl = computed(() => navs.value[navs.value.length - 1])

function updateNavs(list) {
  navs.value = list
}
/**
 * 页面跳转适配处理
 *
 * @format
 */

export const navAdapter = () => {
  const ori = uni.navigateTo
  uni.navigateTo = (options) => {
    // 简单更新一下

    const pages = getCurrentPages()
    if (pages.length > 1) {
      const prevPage: any = pages[pages.length - 2]
      if (prevPage.$page.fullPath === options.url) {
        return uni.navigateBack()
      }
    }

    return ori.call(uni, options).then(() => {
      updateNavs(navs.value.concat(options.url))
    })
  }

  const list = ['reLaunch']

  list.forEach((item) => {
    const tmp = uni[item]
    uni[item] = (options) => {
      return tmp
        .call(uni, options)
        .then(() => updateNavs(navs.value.concat(options.url)))
    }
  })

  const oriBack = uni.navigateBack
  uni.navigateBack = (options) => {
    updateNavs(navs.value.slice(0, navs.value.length - 1))
    return oriBack(options)
  }
}
